#include "gamecoin.h"

#include <QPixmap>
#include <QDebug>

GameCoin::GameCoin(QString img,QWidget *parent)
    : QPushButton(parent)
    , m_isAnimation(false)
    , m_iswin(false)
    , m_minIndex(1)
    , m_maxIndex(8)
{
    //
    if(img !="")
    {
        QPixmap gameCoinpix;
        bool ret = gameCoinpix.load(img);
        if(ret)
        {
            QSize coinsize (gameCoinpix.width(),gameCoinpix.height());
            this->setFixedSize(coinsize);//设置按钮的宽高属性，这里使用this是因为this本身就是button
            this->setStyleSheet("QPushButton{border:0px}");//将btn的边框变为0px
            this->setIcon(gameCoinpix);//将图片插入this
            this->setIconSize(coinsize);//设置插入图片的宽高属性
        }

    }
    //定时器
    m_timerToBack = new QTimer(this);
    connect(m_timerToBack,&QTimer::timeout,this,[=]()
    {
        QString imgCoinStr = QString(":/res/Coin000%1.png").arg(m_minIndex++);
        qDebug() << "to back imgCoinStr : " << imgCoinStr;
        QPixmap imgPix;
        bool retImg = imgPix.load(imgCoinStr);
        if(retImg)
        {
            QSize coinsize (imgPix.width(),imgPix.height());
            this->setFixedSize(coinsize);//设置按钮的宽高属性，这里使用this是因为this本身就是button
            this->setStyleSheet("QPushButton{border:0px}");//将btn的边框变为0px
            this->setIcon(imgPix);//将图片插入this
            this->setIconSize(coinsize);//设置插入图片的宽高属性
        }
        if(m_minIndex > m_maxIndex)
        {
            m_minIndex = 1;
            m_timerToBack->stop();
            m_isAnimation = false;
        }
    });

    m_timerToFront = new QTimer(this);
    connect(m_timerToFront,&QTimer::timeout,this,[=]()
    {
        QString imgCoinStr = QString(":/res/Coin000%8.png").arg(m_maxIndex--);
        qDebug() << "to front imgCoinStr : " << imgCoinStr;
        QPixmap imgPix;
        bool retImg = imgPix.load(imgCoinStr);
        if(retImg)
        {
            QSize coinsize (imgPix.width(),imgPix.height());
            this->setFixedSize(coinsize);//设置按钮的宽高属性，这里使用this是因为this本身就是button
            this->setStyleSheet("QPushButton{border:0px}");//将btn的边框变为0px
            this->setIcon(imgPix);//将图片插入this
            this->setIconSize(coinsize);//设置插入图片的宽高属性
        }
        if(m_maxIndex < m_minIndex)
        {
            m_maxIndex = 8;
            m_timerToFront->stop();
            m_isAnimation = false;
        }
    });


}


void GameCoin::changeFlag()
{
    if(m_flag == true)
    {
        m_timerToBack->start(30);
        m_flag = false;
    }
    else
    {
        m_timerToFront->start(30);
        m_flag = true;
    }
    m_isAnimation = true;
}



int GameCoin::posX() const
{
    return m_posx;
}

void GameCoin::setX(int newposX)
{
    m_posx = newposX;
}



int GameCoin::posY() const
{
    return m_posy;
}

void GameCoin::setY(int newposY)
{
    m_posy = newposY;
}



bool GameCoin::flag() const
{
    return m_flag;
}

void GameCoin::setFlag(bool newFlag)
{
    m_flag = newFlag;
}

bool GameCoin::isWin() const
{
    return m_iswin;
}

void GameCoin::setwin(bool newWin)
{
    m_iswin = newWin;
}
void GameCoin::mousePressEvent(QMouseEvent *e)
{
    if(m_iswin == true || m_isAnimation == true)
    {
        return;
    }
    else
    {
        return QPushButton::mousePressEvent(e);
    }
}
